package Util.Impl;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtil {
    static DataSource ds;
    static{
        try {
            //加载配置文件
            Properties pro=new Properties();
            InputStream is=JdbcUtil.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(is);
            System.out.println(pro);
            //获取连接池对象/数据源
            ds= DruidDataSourceFactory.createDataSource(pro);
            System.out.println("数据库驱动加载成功！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库驱动加载失败！");
        }
    }

    /**
     * 获取数据源
     * @return
     */
    public static DataSource getDataSource(){
        return ds;
    }
    /**
     * 获取数据库连接
     * @return
     */
    public static Connection getConnect(){
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接获取失败");
        }
        return null;
    }
}
